package com.Sumcc.controller;

import com.Sumcc.common.annotation.SignVerify;
import com.Sumcc.common.constant.Constant;

import com.Sumcc.common.result.ResultResponse;
import com.Sumcc.pojo.resp.OSSFileResp;
import com.Sumcc.utils.ossUtil.OSSUtil;
import com.Sumcc.utils.systemUtil.PropertiesUtil;
import com.Sumcc.utils.systemUtil.UUIDUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Sumcc
 * @date 2020/3/19
 */

@RequestMapping(value = Constant.API_PATH+"/file",method = RequestMethod.POST)
@Controller
public class FileUploadController {
    private String deepic = "/src/img";
    /**
     * 单文件上传 不需要签名验证
     * @param file
     * @return
     */
    @SignVerify(value = false)
    @RequestMapping(value="/upload",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultResponse<OSSFileResp>> upload(@RequestParam("file") MultipartFile file){
        try {
            int begin = file.getOriginalFilename().lastIndexOf (".");
            int last = file.getOriginalFilename().length();
            //获得文件后缀名
            String lastName = file.getOriginalFilename().substring(begin, last);
            String fileName= UUIDUtils.getUUID32();
            String OssFileName = fileName+lastName;
            SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
            String format = sdf.format(new Date());
            String realPath = "/upload/wyxt" + format;
            File folder = new File(getProperties()+realPath);
            if (!folder.exists()) {
                folder.mkdirs();
            }
//                OSSUtil.ossUpload(OssFileName,file.getInputStream());
            String oldName = file.getOriginalFilename();
            String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
            file.transferTo(new File(folder,newName));
//            OSSUtil.ossUpload(OssFileName,file.getInputStream());
            OSSFileResp ossFileResp= new OSSFileResp();
            ossFileResp.setFileName(format+newName);
            ossFileResp.setOssFileName(format+newName);
            return ResponseEntity.ok(ResultResponse.ok(ossFileResp));
        }catch (Exception e){
            return ResponseEntity.ok(ResultResponse.fail());
        }
    }

    /**
     * 单文件上传 不需要签名验证
     * @param files
     * @return
     */
    @SignVerify(value = false)
    @RequestMapping(value="/uploadFiles",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultResponse<List<OSSFileResp>>> uploadFiles(@RequestParam("files") MultipartFile[] files){
        try {
            List<OSSFileResp> list = new ArrayList<>();
            for (MultipartFile file: files) {
                int begin = file.getOriginalFilename().lastIndexOf (".");
                int last = file.getOriginalFilename().length();
                //获得文件后缀名
                String lastName = file.getOriginalFilename().substring(begin, last);
                String fileName= UUIDUtils.getUUID32();
                String OssFileName = fileName+lastName;
                SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
                String format = sdf.format(new Date());
                String realPath = "/upload/wyxt" + format;
                File folder = new File(getProperties()+realPath);
                if (!folder.exists()) {
                    folder.mkdirs();
                }
//                OSSUtil.ossUpload(OssFileName,file.getInputStream());
                String oldName = file.getOriginalFilename();
                String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
                file.transferTo(new File(folder,newName));
                OSSFileResp ossFileResp= new OSSFileResp();
                ossFileResp.setFileName(format+newName);
                ossFileResp.setOssFileName(format+newName);
                list.add(ossFileResp);
            }
            return ResponseEntity.ok(ResultResponse.ok(list));
        }catch (Exception e){
            return ResponseEntity.ok(ResultResponse.fail());
        }
    }
    /**
     * 获取盘符地址
     *
     * @return
     */
    public String getProperties() {
        return PropertiesUtil.getStorageAddress();
    }
}
